
import {lsStorage} from '@/common/cache'
import apis from '../../apis/index'

let userInfo = lsStorage.get('userInfo')
let token = lsStorage.get('token')

const state = {
  token: token,
  userInfo: userInfo,
  homeData:""
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    lsStorage.set('token', token, 86400 * 7)
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
    lsStorage.set('userInfo', userInfo, 86400 * 7)
  },
  SET_HOME_DATA: (state, homeData) => {
    state.homeData = homeData
  },
  SetHomeDataRecords: (state, records) => {
    let tempdata={
      records:records
    }
    const newHomeData = {
      ...state.homeData,
      ...tempdata
    }
    state.homeData = newHomeData
  },
}

const actions = {
  logout ({commit, state, dispatch}) {
    return new Promise((resolve, reject) => {
      commit('SET_USER_INFO', null)
      commit('SET_TOKEN', null)
      resolve()
    })
  },
  updateToken({commit, state, dispatch}, token) {
    return new Promise((resolve, reject) => {
      //console.log(token);
      commit('SET_TOKEN', token)
      resolve()
    })
  },
  updateUserInfo ({commit, state, dispatch}, userInfo) {
    return new Promise((resolve, reject) => {
      const oldUserInfo = state.userInfo
      const newUserInfo = {
        ...oldUserInfo,
        ...userInfo
      }
      commit('SET_USER_INFO', newUserInfo)
      resolve()
    })
  },
  updateHomeData ({commit, state, dispatch}) {
    apis.homeData().then(res=>{
      const oldHomeData = state.homeData

      const newHomeData = {
        ...oldHomeData,
        ...res
      }

      commit('SET_HOME_DATA', newHomeData)
    })
    // return new Promise((resolve, reject) => {
    //   req.historyShowReoprt(obj).then(data => {
    //     commit('reopreData',data);
    //     resolve(true)
    //   })
    // })


  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
